package com.shijue;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * @author Zhang Zhe
 * @since 2019年12月05日 16:57:00
 */
public class StudioTest {

	public static  CookieStore cookieStore;
	/**
	 * 学生免审加入鲁美工作室.
	 * @param
	 * @return void .
	 * @author Zhang Zhe
	 * @since 2019/12/5 4:54 PM
	 */
	public static String getJsonData(JSONObject jsonParam, String urls, String cookie) {
		StringBuffer sb=new StringBuffer();
		try {
			;
			// 创建url资源
			URL url = new URL(urls);
			// 建立http连接
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置允许输出
			conn.setDoOutput(true);
			// 设置允许输入
			conn.setDoInput(true);
			// 设置不用缓存
			conn.setUseCaches(false);
			// 设置传递方式
			conn.setRequestMethod("POST");
			// 设置维持长连接
			conn.setRequestProperty("Connection", "Keep-Alive");
			//设置cookie
			conn.setRequestProperty("Cookie", cookie);
			// 设置文件字符集:
			conn.setRequestProperty("Charset", "UTF-8");
			// 转换为字节数组
			byte[] data = (jsonParam.toString()).getBytes();
			// 设置文件长度
			conn.setRequestProperty("Content-Length", String.valueOf(data.length));
			// 设置文件类型:
			conn.setRequestProperty("Content-Type", "application/json");
			// 开始连接请求
			conn.connect();
			OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
			// 写入请求的字符串
			out.write((jsonParam.toString()).getBytes());
			out.flush();
			out.close();

			System.out.println(conn.getResponseCode());

			// 请求返回的状态
			if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
				System.out.println("连接成功");
				// 请求返回的数据
				InputStream in1 = conn.getInputStream();
				try {
					String readLine=new String();
					BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
					while((readLine=responseReader.readLine())!=null){
						sb.append(readLine).append("\n");
					}
					responseReader.close();
					System.out.println(sb.toString());

				} catch (Exception e1) {
					e1.printStackTrace();
				}
			} else {
				System.out.println("error++");

			}

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("message:"+e.getMessage());
		}

		return sb.toString();

	}

	public static String entityToString(HttpEntity entity) throws IOException {
		String result = null;
		if(entity != null)
		{
			long lenth = entity.getContentLength();
			if(lenth != -1 && lenth < 2048)
			{
				result = EntityUtils.toString(entity,"UTF-8");
			}else {
				InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
				CharArrayBuffer buffer = new CharArrayBuffer(2048);
				char[] tmp = new char[1024];
				int l;
				while((l = reader1.read(tmp)) != -1) {
					buffer.append(tmp, 0, l);
				}
				result = buffer.toString();
			}
		}
		return result;
	}

	/**
	 * get请求，参数拼接在地址上
	 * @param url 请求地址加参数
	 * @return 响应
	 */
	public static String get(String url)
	{

		CloseableHttpClient httpClient =null;
		cookieStore = new BasicCookieStore();
		httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

		String result = null;

		HttpGet get = new HttpGet(url);
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(get);
			if(response != null && response.getStatusLine().getStatusCode() == 200)
			{
				HttpEntity entity = response.getEntity();
				result = entityToString(entity);
			}
			return result;
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				httpClient.close();
				if(response != null)
				{
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/*public static void main(String[] args) {


		//---------------------------


		JSONObject jsonParam = new JSONObject();
		//用户账号
		String[] account = new String[]{
				"15140466761",
				"13204076777",
				"18940850931",
				"18940854249",
				"13131888318",
				"15326166255",
				"15641450724",
				"15940119362",
				"18940887279",
				"15295875916",
				"15504968670",
				"15382139323",
				"18940855623",
				"13236916628",
				"18525469352",
				"13591783460",
				"17615118626"
		};

		String[] account1 = new String[]{
				"13019978180",
				"17770310512",
				"18941191627",
				"15713870925",
				"15309812328",
				"18098867302",
				"13855191898",
				"18941192271",
				"18941192100",
				"13354287913",
				"17640086412",
				"13354287703",
				"17640337447",
				"13840416422",
				"18125543186",
				"18040113129"
		};
		//String password = "Zz05186924132";
		String password = "123456";
		//姓名
		String[] names = new String[]{
				"张延帅",
				"杨泳琪",
				"黄鹏",
				"崔慧卿",
				"杨泽铭",
				"张昕",
				"杜雨霏",
				"何家新",
				"郭雨芯",
				"戴茵",
				"齐裕",
				"王玉芳",
				"吴兴铂",
				"张乃蘅",
				"彭语涵",
				"赵军涛",
				"刘瑞林"
		};

		String [] names1 = new String[]{
				"韩楚阳",
				"张可儿",
				"赵九洲",
				"王安妮",
				"徐良伟",
				"邢元磊",
				"汪欣琪",
				"邹德睿",
				"王俊尧",
				"谭志轩",
				"李睿",
				"周品池",
				"庞中甫",
				"张欣也",
				"陈骅琪",
				"刘乐"
		};
		//学号
		String[] nos = new String[]{
				"2014020402",
				"2015020304",
				"2016020115",
				"2016020133",
				"2016020207",
				"2016020208",
				"2016020224",
				"2016020404",
				"2016020508",
				"2016020509",
				"2016020518",
				"2016020521",
				"2016020528",
				"2016020539",
				"2016020540",
				"2016020541",
				"2016020631"

		};

		String[] nos1 = new String[]{
				"2017020106",
				"2017020110",
				"2017020116",
				"2017020117",
				"2017020121",
				"2017020124",
				"2017020127",
				"2017020130",
				"2017020134",
				"2017020136",
				"2017020209",
				"2017020231",
				"2017020415",
				"2017020440",
				"2017020502",
				"2017020526"

		};

		//工作室ID
		String studioId = "402880bc69fb97fd016a5328dba00197";
		//年纪
		String grade = "2016";
		String grade1 = "2017";

		try {
			for (int i=0;i < names1.length;i++){
				//调登陆接口---begin
				String user = StudioTest.get("http://shijue.me/user/v2/tologin?userName="+account1[i]+"&countryCode=86&password="+password);
				com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(user);
				com.alibaba.fastjson.JSONObject userObject = (com.alibaba.fastjson.JSONObject) jsonObject.get("userAccountInfo");
				String userId = userObject.getString("userId");
				List<Cookie> cookies = cookieStore.getCookies();
				String cookie = "";
				for (int j = 0; j < cookies.size(); j++) {
					String cook= cookies.get(j).getName()+"="+cookies.get(j).getValue();
					cookie=cookie+cook;
					if(j < cookies.size()-1){
						cookie = cookie+"; ";
					}
				}
				//调登陆接口----end

				//加入工作室接口----begin
				jsonParam.put("studioId", studioId);
				jsonParam.put("studentName", names1[i]);
				jsonParam.put("studentSchool", "");
				jsonParam.put("studentSpecialty", "");
				jsonParam.put("studentGrade", grade);
				jsonParam.put("role", "student");
				jsonParam.put("collegeDuty", "");
				jsonParam.put("studentId", nos1[i]);
				jsonParam.put("applyReason", "");
				jsonParam.put("frameworkId", "1001");

				String url="http://shijue.me/studios/members/apply_join_studio2";
				String data=StudioTest.getJsonData(jsonParam,url,cookie);
				//加入工作室接口--end


			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}*/

	public static void main(String[] args) {
		String[] mems = new String[]{
				"402880ba6e90ea36016ed639153e0055",
				"402880bc6e90e474016ed63916ff004a",
				"402880ba6e90ea36016ed63911f20053",
				"402880bc6e90e474016ed63913ab0048",
				"402880bc6e90e474016ed6390edc0046",
				"402880ba6e90ea36016ed6390d40004f",
				"402880ba6e90ea36016ed63910730051",
				"402880ba6e90ea36016ed63909c3004d",
				"402880bc6e90e474016ed6390ba50044",
				"402880bc6e90e474016ed63906d90040",
				"402880bc6e90e474016ed63908730042",
				"402880ba6e90ea36016ed639040a004b",
				"402880bc6e90e474016ed6390271003c",
				"402880bc6e90e474016ed6390556003e",
				"402880bc6e90e474016ed63900d8003a",
				"402880bc6e90e474016ed638ff410038",
				"402880ba6e90ea36016ed637d4d40047",
				"402880ba6e90ea36016ed637d7a20049",
				"402880bc6e90e474016ed637d6390036",
				"402880ba6e90ea36016ed637d33f0045",
				"402880bc6e90e474016ed637d1cc0034",
				"402880ba6e90ea36016ed637d0710043",
				"402880bc6e90e474016ed637ced70032",
				"402880ba6e90ea36016ed637cd510041",
				"402880bc6e90e474016ed637ca210030",
				"402880ba6e90ea36016ed637cba2003f",
				"402880bc6e90e474016ed637c8d8002e",
				"402880ba6e90ea36016ed637c73a003d",
				"402880ba6e90ea36016ed637c5e4003b",
				"402880ba6e90ea36016ed637c26f0039",
				"402880bc6e90e474016ed637c414002c",
				"402880ba6e90ea36016ed637c0e90037"/*,
				"402880ba6e90ea36016ed637bfa70035"*/
		};
		//小组id
		String groupId = "402880bc6b8825b7016b8891f9230000";
		String cookie = "acw_tc=2760823a15755337506568948e3a65e63f068c36a1993d522ba8c701ba29c1; SESSION=e1ff1bf8-fe3b-4a9d-9cac-f8c5ef80e466; Hm_lvt_7acfbc4d7bdb93be5cf0ee26c52160bf=1575533752; ticket=6B7E8DE33C6F30B852F67072C7C97685E2D03DEF442D0AC3DB44B64D4A85E18AF4EC9A529E20E48007F0A2CA0281D85F; userId=ecb3390c349f5a57b3b64f790537e9664_d; token=Token-878c15d8-fd63-4233-9b11-4301390c4ca5; Hm_lpvt_7acfbc4d7bdb93be5cf0ee26c52160bf=1575552877; testapp=s%3ABwZ5-hG3x9tPNz2mNdDhAeWHjh7PkwIj.l5F1I8sOiP1pDSAvmz2ZLHEhueVcaYKQBCspi1F6TiI";
		//加入小组接口--begin


				JSONArray memberIds = new JSONArray();
				for (String mem:mems){
					memberIds.add(mem);
				}
				JSONObject group = new JSONObject();
				group.put("groupId",groupId);
				group.put("memberIds",memberIds);
				String groupUrl = "http://shijue.me/studios/members/group/add_members_group";
				StudioTest.getJsonData(group,groupUrl,cookie);
		//加入小组接口--end
	}

}
